package com.zyx.jdbcdemo.utils;


import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Yaxi.Zhang
 * @since 2021/4/29 13:30
 */
public class QueryRunnerUtil {
    /**
     * Jdbc查询结果并返回
     *
     * @param conn    Jdbc连接
     * @param sql     查询sql语句
     * @param args    占位符参数
     * @param tClass  结果类型
     * @param isCamel 是否需要将下划线转成驼峰命名
     * @param <T>     结果类型的泛型
     * @return 结果列表
     */
    protected static <T> List<T> queryList(Connection conn,
                                        String sql,
                                        Object[] args,
                                        Class<T> tClass,
                                        boolean isCamel) {
        QueryRunner qr = new QueryRunner();
        List<T> queryResult = new ArrayList<>();
        try {
            // 进行查询
            if (args == null) {
                if (isCamel) {
                    queryResult = qr.query(conn, sql, new BeanListHandler<>(
                            tClass, new BasicRowProcessor(new GenerousBeanProcessor())));
                } else {
                    queryResult = qr.query(conn, sql, new BeanListHandler<>(tClass));
                }
            } else {
                if (isCamel) {
                    queryResult = qr.query(conn, sql, new BeanListHandler<>(
                            tClass, new BasicRowProcessor(new GenerousBeanProcessor())), args);
                } else {
                    queryResult = qr.query(conn, sql, new BeanListHandler<>(tClass), args);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 关闭资源
            try {
                DbUtils.close(conn);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return queryResult;
    }

    /**
     * 将对象的列表插入到数据库中
     *
     * @param conn 对象的连接
     * @param sql  插入语句
     * @param list 对象列表
     * @param <T>  对象的泛型
     */
    protected static <T> void insertList(Connection conn,
                                         String sql,
                                         List<T> list) {
        QueryRunner qr = new QueryRunner();
        try {
            qr.batch(conn, sql, CommonUtil.list2DimObjArray(list));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 关闭资源
            try {
                DbUtils.close(conn);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    protected static <T> void update(Connection conn,
                                     String sql) {
        QueryRunner qr = new QueryRunner();
        try {
            qr.update(conn, sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                DbUtils.close(conn);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}